{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "In C:\\Users\\Han\\.conda\\envs\\my_project1\\lib\\site-packages\\matplotlib\\mpl-data\\stylelib\\_classic_test.mplstyle: \n",
      "The text.latex.preview rcparam was deprecated in Matplotlib 3.3 and will be removed two minor releases later.\n",
      "In C:\\Users\\Han\\.conda\\envs\\my_project1\\lib\\site-packages\\matplotlib\\mpl-data\\stylelib\\_classic_test.mplstyle: \n",
      "The mathtext.fallback_to_cm rcparam was deprecated in Matplotlib 3.3 and will be removed two minor releases later.\n",
      "In C:\\Users\\Han\\.conda\\envs\\my_project1\\lib\\site-packages\\matplotlib\\mpl-data\\stylelib\\_classic_test.mplstyle: Support for setting the 'mathtext.fallback_to_cm' rcParam is deprecated since 3.3 and will be removed two minor releases later; use 'mathtext.fallback : 'cm' instead.\n",
      "In C:\\Users\\Han\\.conda\\envs\\my_project1\\lib\\site-packages\\matplotlib\\mpl-data\\stylelib\\_classic_test.mplstyle: \n",
      "The validate_bool_maybe_none function was deprecated in Matplotlib 3.3 and will be removed two minor releases later.\n",
      "In C:\\Users\\Han\\.conda\\envs\\my_project1\\lib\\site-packages\\matplotlib\\mpl-data\\stylelib\\_classic_test.mplstyle: \n",
      "The savefig.jpeg_quality rcparam was deprecated in Matplotlib 3.3 and will be removed two minor releases later.\n",
      "In C:\\Users\\Han\\.conda\\envs\\my_project1\\lib\\site-packages\\matplotlib\\mpl-data\\stylelib\\_classic_test.mplstyle: \n",
      "The keymap.all_axes rcparam was deprecated in Matplotlib 3.3 and will be removed two minor releases later.\n",
      "In C:\\Users\\Han\\.conda\\envs\\my_project1\\lib\\site-packages\\matplotlib\\mpl-data\\stylelib\\_classic_test.mplstyle: \n",
      "The animation.avconv_path rcparam was deprecated in Matplotlib 3.3 and will be removed two minor releases later.\n",
      "In C:\\Users\\Han\\.conda\\envs\\my_project1\\lib\\site-packages\\matplotlib\\mpl-data\\stylelib\\_classic_test.mplstyle: \n",
      "The animation.avconv_args rcparam was deprecated in Matplotlib 3.3 and will be removed two minor releases later.\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "import graphviz\n",
    "import lingam\n",
    "from lingam.utils import make_dot\n",
    "import logging\n",
    "import dowhy\n",
    "from dowhy import CausalModel\n",
    "from semopy import Model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.read_excel(r\"C:\\Users\\Han\\Desktop\\document\\불평등 연구\\데이터\\33_불평등_외로움_사회분열\\df_merge_1.xlsx\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>year</th>\n",
       "      <th>sido</th>\n",
       "      <th>Inequality</th>\n",
       "      <th>Loneliness</th>\n",
       "      <th>Distrust</th>\n",
       "      <th>Exploitation</th>\n",
       "      <th>Indifference</th>\n",
       "      <th>income</th>\n",
       "      <th>education</th>\n",
       "      <th>housing_type</th>\n",
       "      <th>occupation</th>\n",
       "      <th>age</th>\n",
       "      <th>sex</th>\n",
       "      <th>marriage</th>\n",
       "      <th>religion</th>\n",
       "      <th>ideology</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>2020</td>\n",
       "      <td>인천</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>5</td>\n",
       "      <td>5.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>9.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>2020</td>\n",
       "      <td>서울</td>\n",
       "      <td>5.0</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>4.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   id  year sido  Inequality  Loneliness  Distrust  Exploitation  \\\n",
       "0   1  2020   인천         3.0           1         3             3   \n",
       "1   2  2020   서울         5.0           1         4             2   \n",
       "\n",
       "   Indifference  income  education  housing_type  occupation  age  sex  \\\n",
       "0             3       5        5.0             1           0    3    0   \n",
       "1             3       4        3.0             1           0    4    1   \n",
       "\n",
       "   marriage  religion  ideology  \n",
       "0         1         1       9.0  \n",
       "1         1         0       4.0  "
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head(2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1. Path analysis visualization"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Define the SEM model\n",
    "model_desc = \"\"\"\n",
    "Distrust ~ Loneliness + income + education + housing_type + occupation + age + sex + marriage + religion + ideology\n",
    "Exploitation ~ Loneliness + income + education + housing_type + occupation + age + sex + marriage + religion + ideology\n",
    "Indifference ~ Loneliness + income + education + housing_type + occupation + age + sex + marriage + religion + ideology\n",
    "Loneliness ~ Inequality + income + education + housing_type + occupation + age + sex + marriage + religion + ideology\n",
    "\"\"\"\n",
    "\n",
    "# Create and fit the model\n",
    "model = Model(model_desc)\n",
    "results = model.fit(df)\n",
    "params = model.inspect()\n",
    "\n",
    "# Function to visualize the SEM model with parameter estimates\n",
    "def visualize_sem_with_focused_params(model, params, focus_vars, path=\"sem_graph\", width=\"80\", height=\"180\", fontsize=\"18\"):\n",
    "    dot = graphviz.Digraph(comment='Focused SEM Model')\n",
    "    dot.attr(size=f\"{width},{height}\", ratio='auto')\n",
    "\n",
    "    # Define the set of variables to focus on\n",
    "    focus_set = set(focus_vars)\n",
    "\n",
    "    # Adding nodes for the focused variables\n",
    "    for var in focus_set:\n",
    "        dot.node(var, var, shape='ellipse')\n",
    "\n",
    "    # Adding edges with parameter estimates for the focused variables\n",
    "    for index, row in params.iterrows():\n",
    "        if row['op'] == '~' and {row['lval'], row['rval']}.issubset(focus_set):\n",
    "            estimate = round(row['Estimate'], 3)\n",
    "            p_value = round(row['p-value'], 3)\n",
    "            significance = '***' if p_value < 0.01 else '**' if p_value < 0.05 else ''\n",
    "            label = f'est: {estimate}, p: {p_value} {significance}'\n",
    "            dot.edge(row['rval'], row['lval'], label=label, fontsize=fontsize)\n",
    "\n",
    "    # Save and render the graph\n",
    "    dot.render(path, view=True)\n",
    "\n",
    "# Specify the focus variables\n",
    "focus_vars = [\"Inequality\", \"Loneliness\", \"Distrust\", \"Exploitation\", \"Indifference\"]\n",
    "\n",
    "# Visualize the SEM model focused on specified variables\n",
    "visualize_sem_with_focused_params(model, params, focus_vars, \"focused_sem_example\", width=\"80\", height=\"180\", fontsize=\"18\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2. Path analysis full results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "            lval  op          rval  Estimate  Std. Err     z-value  \\\n",
      "0     Loneliness   ~    Inequality  0.020397  0.002708    7.530780   \n",
      "1     Loneliness   ~        income -0.011815  0.001761   -6.707747   \n",
      "2     Loneliness   ~     education -0.017743  0.003070   -5.778592   \n",
      "3     Loneliness   ~  housing_type -0.095003  0.007182  -13.227780   \n",
      "4     Loneliness   ~    occupation  0.033518  0.020659    1.622452   \n",
      "5     Loneliness   ~           age  0.028444  0.002322   12.252294   \n",
      "6     Loneliness   ~           sex  0.009597  0.006081    1.578274   \n",
      "7     Loneliness   ~      marriage -0.073327  0.005636  -13.009393   \n",
      "8     Loneliness   ~      religion  0.021895  0.006186    3.539246   \n",
      "9     Loneliness   ~      ideology  0.014532  0.001562    9.306011   \n",
      "10      Distrust   ~    Loneliness  0.094731  0.005796   16.343613   \n",
      "11      Distrust   ~        income -0.016571  0.002244   -7.384022   \n",
      "12      Distrust   ~     education -0.014514  0.003911   -3.711433   \n",
      "13      Distrust   ~  housing_type -0.132874  0.009160  -14.506606   \n",
      "14      Distrust   ~    occupation  0.074948  0.026311    2.848481   \n",
      "15      Distrust   ~           age -0.002754  0.002961   -0.930136   \n",
      "16      Distrust   ~           sex -0.027628  0.007744   -3.567382   \n",
      "17      Distrust   ~      marriage  0.011773  0.007176    1.640565   \n",
      "18      Distrust   ~      religion -0.046881  0.007879   -5.950224   \n",
      "19      Distrust   ~      ideology -0.013499  0.001986   -6.796151   \n",
      "20  Exploitation   ~    Loneliness  0.127500  0.007078   18.014061   \n",
      "21  Exploitation   ~        income  0.023682  0.002740    8.641997   \n",
      "22  Exploitation   ~     education  0.020181  0.004775    4.226163   \n",
      "23  Exploitation   ~  housing_type  0.001120  0.011185    0.100152   \n",
      "24  Exploitation   ~    occupation -0.079104  0.032129   -2.462073   \n",
      "25  Exploitation   ~           age -0.017610  0.003615   -4.871022   \n",
      "26  Exploitation   ~           sex  0.020080  0.009457    2.123344   \n",
      "27  Exploitation   ~      marriage  0.005817  0.008763    0.663853   \n",
      "28  Exploitation   ~      religion -0.096713  0.009621  -10.052456   \n",
      "29  Exploitation   ~      ideology  0.038601  0.002425   15.914855   \n",
      "30  Indifference   ~    Loneliness  0.066458  0.005739   11.580879   \n",
      "31  Indifference   ~        income -0.014775  0.002222   -6.650137   \n",
      "32  Indifference   ~     education -0.011116  0.003872   -2.871084   \n",
      "33  Indifference   ~  housing_type -0.083578  0.009069   -9.216206   \n",
      "34  Indifference   ~    occupation  0.003894  0.026050    0.149480   \n",
      "35  Indifference   ~           age  0.005365  0.002931    1.830097   \n",
      "36  Indifference   ~           sex -0.031139  0.007668   -4.061118   \n",
      "37  Indifference   ~      marriage  0.016423  0.007105    2.311500   \n",
      "38  Indifference   ~      religion -0.040086  0.007800   -5.138919   \n",
      "39  Indifference   ~      ideology -0.015156  0.001967   -7.707086   \n",
      "40    Loneliness  ~~    Loneliness  0.373428  0.002405  155.283611   \n",
      "41      Distrust  ~~      Distrust  0.605743  0.003901  155.283611   \n",
      "42  Exploitation  ~~  Exploitation  0.903218  0.005817  155.283611   \n",
      "43  Indifference  ~~  Indifference  0.593763  0.003824  155.283611   \n",
      "\n",
      "         p-value  \n",
      "0   5.040413e-14  \n",
      "1   1.976530e-11  \n",
      "2   7.532843e-09  \n",
      "3   0.000000e+00  \n",
      "4   1.047067e-01  \n",
      "5   0.000000e+00  \n",
      "6   1.145027e-01  \n",
      "7   0.000000e+00  \n",
      "8   4.012712e-04  \n",
      "9   0.000000e+00  \n",
      "10  0.000000e+00  \n",
      "11  1.536549e-13  \n",
      "12  2.060896e-04  \n",
      "13  0.000000e+00  \n",
      "14  4.392842e-03  \n",
      "15  3.523006e-01  \n",
      "16  3.605659e-04  \n",
      "17  1.008878e-01  \n",
      "18  2.677764e-09  \n",
      "19  1.074518e-11  \n",
      "20  0.000000e+00  \n",
      "21  0.000000e+00  \n",
      "22  2.377098e-05  \n",
      "23  9.202238e-01  \n",
      "24  1.381365e-02  \n",
      "25  1.110226e-06  \n",
      "26  3.372504e-02  \n",
      "27  5.067843e-01  \n",
      "28  0.000000e+00  \n",
      "29  0.000000e+00  \n",
      "30  0.000000e+00  \n",
      "31  2.928213e-11  \n",
      "32  4.090663e-03  \n",
      "33  0.000000e+00  \n",
      "34  8.811748e-01  \n",
      "35  6.723537e-02  \n",
      "36  4.883828e-05  \n",
      "37  2.080525e-02  \n",
      "38  2.763232e-07  \n",
      "39  1.287859e-14  \n",
      "40  0.000000e+00  \n",
      "41  0.000000e+00  \n",
      "42  0.000000e+00  \n",
      "43  0.000000e+00  \n"
     ]
    }
   ],
   "source": [
    "# Define the SEM model\n",
    "model_desc = \"\"\"\n",
    "Distrust ~ Loneliness + income + education + housing_type + occupation + age + sex + marriage + religion + ideology\n",
    "Exploitation ~ Loneliness + income + education + housing_type + occupation + age + sex + marriage + religion + ideology\n",
    "Indifference ~ Loneliness + income + education + housing_type + occupation + age + sex + marriage + religion + ideology\n",
    "Loneliness ~ Inequality + income + education + housing_type + occupation + age + sex + marriage + religion + ideology\n",
    "\"\"\"\n",
    "\n",
    "\n",
    "# Create and fit the model\n",
    "model = Model(model_desc)\n",
    "results = model.fit(df)\n",
    "\n",
    "# Inspect the model's results\n",
    "params = model.inspect()\n",
    "\n",
    "# Display the results\n",
    "print(params)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
